﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Administrator_AdminControl_LuckCycleSetting : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ltlAlert.Text = "";
        if (!Page.IsPostBack)
        {
            LoadRiseInfo();
            OnClientClick();
        }
    }
    protected void rptRiseLucky_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            long LuckCycleSetting_Id = long.Parse(e.CommandArgument.ToString());
            if (Delete(LuckCycleSetting_Id))
            {   
                ShowMessage("Xóa dữ liệu thành công");
                LoadRiseInfo();
            }
            else
            {
                ShowMessage("Xóa dữ liệu không thành công");
            }
        }

        if (e.CommandName == "Edit")
        {
            if (((TextBox)e.Item.FindControl("txtPoint")).Visible == true)
            {
                long LuckCycleSetting_Id = long.Parse(e.CommandArgument.ToString());
                int LuckCycleSetting_Point = int.Parse(((TextBox)e.Item.FindControl("txtPoint")).Text);
                double LuckCycleSetting_Time = double.Parse(((TextBox)e.Item.FindControl("txtTime")).Text);
                if (Update(LuckCycleSetting_Id, LuckCycleSetting_Point, LuckCycleSetting_Time))
                {
                    LoadRiseInfo();                    
                }                
            }
            else
            {
                ((Label)e.Item.FindControl("lblPoint")).Visible = false;
                ((Label)e.Item.FindControl("lblTime")).Visible = false;
                ((TextBox)e.Item.FindControl("txtPoint")).Visible = true;
                ((TextBox)e.Item.FindControl("txtTime")).Visible = true;
                ((ImageButton)e.Item.FindControl("bntRefresh")).Enabled = true;
                ((ImageButton)e.Item.FindControl("bntRefresh")).ImageUrl = "~/images/Admin/ActiveRefresh.png";

                GenerateClientScriptForRepeaterKeyDown();
            }
        }

        if (e.CommandName == "Refresh")
        {
            ((Label)e.Item.FindControl("lblPoint")).Visible = true;
            ((Label)e.Item.FindControl("lblTime")).Visible = true;
            ((TextBox)e.Item.FindControl("txtPoint")).Visible = false;
            ((TextBox)e.Item.FindControl("txtTime")).Visible = false;
            ((ImageButton)e.Item.FindControl("bntRefresh")).ImageUrl = "~/images/Admin/InactiveRefresh.png";
            GenerateClientScriptForRepeaterKeyDown();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        bool result = Insert();
        if (result)
        {
            LoadRiseInfo();
        }
    }

    private void OnClientClick()
    {
        string strScript = string.Empty;
        strScript = "return RiseCylce_CheckValue($('#" + txtPoint.ClientID + "').val(),$('#" + txtTime.ClientID + "').val());";
        btnSave.OnClientClick = strScript;
    }

    private void GenerateClientScriptForRepeaterKeyDown()
    {
        string strClientScript = string.Empty;
        strClientScript += "<script type='text/javascript'>";
        strClientScript += "$(document).ready(function () {";

        foreach (RepeaterItem item in rptRiseLucky.Items)
        {
            if (item.FindControl("txtPoint").Visible)
            {
                strClientScript += "$('#" + item.FindControl("txtPoint").ClientID + "').keydown(function (event) {";
                strClientScript += "KeyDown_IsInteger($('#" + item.FindControl("txtPoint").ClientID + "').val(), event); });";

                ((ImageButton)item.FindControl("btnEdit")).OnClientClick = "return RiseCylce_CheckValue($('#" + item.FindControl("txtPoint").ClientID + "').val(),$('#" + item.FindControl("txtTime").ClientID + "').val());";
            }
            else
            {
                ((ImageButton)item.FindControl("btnEdit")).OnClientClick = "";
            }

            if (item.FindControl("txtTime").Visible)
            {
                strClientScript += "$('#" + item.FindControl("txtTime").ClientID + "').keydown(function (event) {";
                strClientScript += "KeyDown_IsFloat($('#" + item.FindControl("txtTime").ClientID + "').val(), event); });";
            }
        }

        strClientScript += "});";
        strClientScript += "</script>";

        ltlScript.Text = strClientScript;
    }

    private void LoadRiseInfo()
    {
        try
        {
            rptRiseLucky.DataSource = null;

            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = (from o in dc.LuckCycleSettings orderby o.LuckCycleSetting_Point select o).ToList();
                if (query != null)
                {
                    PagedDataSource page = new PagedDataSource();
                    page.DataSource = query;
                    rptRiseLucky.DataSource = page;
                    rptRiseLucky.DataBind();
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private bool Insert()
    {
        bool result = true;
        try
        {
            int LuckCycleSetting_Point = int.Parse(txtPoint.Text);
            double LuckCycleSetting_Time = double.Parse(txtTime.Text);
            short LuckCycleSetting_Type = LuckCycleSettingType.Increase;

            Up1VNDataContext dc = new Up1VNDataContext();
            long? LuckCycleSetting_Id = 0;
            dc.USP_LuckCycleSetting_Insert(ref LuckCycleSetting_Id, LuckCycleSetting_Point, LuckCycleSetting_Time, LuckCycleSetting_Type);

            if (LuckCycleSetting_Id == -1)
            {
                ShowMessage("Lưu dữ liệu không thành công");
                result = false;
            }
            else if (LuckCycleSetting_Id == -2)
            {
                ShowMessage("Vui lòng nhập điểm không nằm trong danh sách");
                result = false;
            }
            else if (LuckCycleSetting_Id > 0)
            {
                LoadRiseInfo();
            }

        }
        catch (Exception ex)
        {
            result = false;
        }

        return result;
    }

    private bool Update(long LuckCycleSetting_Id, int LuckCycleSetting_Point, double LuckCycleSetting_Time)
    {
        bool result = true;
        try
        {   
            short LuckCycleSetting_Type = LuckCycleSettingType.Increase;            

            Up1VNDataContext dc = new Up1VNDataContext();
            int? resultInt = 0;
            dc.USP_LuckCycleSetting_Update(LuckCycleSetting_Id, LuckCycleSetting_Point, LuckCycleSetting_Time, LuckCycleSetting_Type, ref resultInt);

            if (resultInt == -1)
            {
                ShowMessage("Cập nhật dữ liệu không thành công");
                result = false;
            }
            else if (resultInt == -2)
            {
                ShowMessage("Vui lòng nhập điểm không nằm trong danh sách");
                result = false;
            }
            else if(resultInt > 0)
            {
                LoadRiseInfo();
            }
        }
        catch (Exception ex)
        {
            result = false;
        }

        return result;
    }

    private bool Delete(long LuckCycleSetting_Id)
    {
        bool result = true;
        try
        {
            using (Up1VNDataContext dc = new Up1VNDataContext())
            {
                var query = from o in dc.LuckCycleSettings where o.LuckCycleSetting_Id == LuckCycleSetting_Id select o;//long.Parse(e.CommandArgument.ToString()) select o;
                dc.LuckCycleSettings.DeleteAllOnSubmit(query);
                dc.SubmitChanges();
                LoadRiseInfo();
            }
        }
        catch (Exception ex)
        {
            result = false;
        }
        return result;
    }

    private void ShowMessage(string strMessage)
    {
        string strScript ="alert('" + strMessage + "');";
        //ScriptManager.RegisterStartupScript(UpdatePanel1, UpdatePanel1.GetType(), "key", strScript, true);
    }
}